#pragma once

#include <gpiod.h>

#include <string>
#include <chrono>
#include <thread>

#include <stdio.h>
#include <unistd.h>
#include <libserialport.h>

#include <iostream>
#include <vector>

void t_thread();

class uart_v1
{

private:
    std::string _port_name;
    struct sp_port *_port;
    int _baud_rate;
    int _data_bits;
    int _stop_bits;

    uint32_t _byte_timeout_ms;

    sp_return open_flag; // 0:open success 1:open failed

public:
    uart_v1(std::string port_name, int baud_rate, int data_bits, int stop_bits);
    ~uart_v1();

    sp_return uart_open();
    sp_return uart_close();
    int uart_send(const uint8_t *data, int len);
    int uart_recv(uint8_t *data, int len);
    int uart_recv_one_by_one(uint8_t *data, int max_len, int frame_timeout_ms);

    void uart_change_baud_rate(int baud_rate);
    void uart_flush_all();
    std::string uart_get_port_name();
    void uart_print_config();
};
